﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
using System.Linq;
public class DN100GameOver : MonoBehaviour {
    //天地玄黄的输赢情况
    public List<UISprite> winLoseSprites;
    public GameObject tongSha;

    public GameObject bankerScoreSprite;
    public GameObject bankerScoreFather;
    public UISprite bankerJiaJian;

    public GameObject ownScoreSprite;
    public GameObject ownScoreFather;
    public UISprite ownJiaJian;

    public int bankerScore;
    public int ownScore;
    public int ownCoinCount;
    private GameObject cardsPanel;    
    private BankerInfo bankerInfo;
    private DNHundredPanel dnHundredPanel;
    public List<DNHundredUser> users;
    public Nn100GameOverBroadcast newpb;

    private DNHundredGame dnGame;
	void Start () {
        dnGame = GameObject.Find("UI Root/Camera").GetComponent<DNHundredGame>();
        cardsPanel = GameObject.Find("UI Root/CardsPanel");
        dnHundredPanel = GameObject.Find("UI Root/DNPanel100").GetComponent<DNHundredPanel>();

        for (int i = 0; i < 4;i++)
        {
            users[i] = GameObject.Find("UI Root/DNPanel100/Players/Player" + (i + 1)).GetComponent<DNHundredUser>();
            Debug.Log("users[i].name = " + users[i].name);
        }
        bankerInfo = GameObject.Find("UI Root/DNPanel100/Banker").GetComponent<BankerInfo>();
        //显示庄家和自己的得分
        ShowScore(bankerScore, bankerScoreSprite, bankerScoreFather, bankerJiaJian);
        ShowScore(ownScore, ownScoreSprite, ownScoreFather, ownJiaJian);

        //Destroy(gameObject,2f);

        StartCoroutine(ResetBankerAndChairUser(newpb));
	}


    /// <summary>
    /// 显示加减的分数
    /// </summary>
    void ShowScore(int score,GameObject obj1,GameObject obj1Father,UISprite sprite)
    {
        string plusLess = "";
        float x = obj1.transform.localPosition.x;
        float y = obj1.transform.localPosition.y;
        string tempStr = EginUser.Instance.ReturnMoney(Mathf.Abs(score));
        //Debug.Log(tempStr + "   " + bankerScore);
        //Debug.Log(tempStr + "   " + ownScore);
        int len = tempStr.Length;
        //分数大于0
        if(score > 0){
            plusLess = "plus_";
            //显示+
            sprite.gameObject.SetActive(true);
            sprite.spriteName = "plus_" + "jia";
        }
        else
        {
            plusLess = "less_";
            //显示+
            sprite.gameObject.SetActive(true);
            sprite.spriteName = "less_" + "jian";
        }        
        //设置分数
        for (int i = 0; i < len; i++)
        {
            GameObject obj = Instantiate(obj1);
            obj.transform.parent = obj1Father.transform;
            obj.transform.localScale = Vector3.one;
            obj.SetActive(true);
            string str = tempStr.Substring(0, 1);
            //Debug.Log(str);
            if (i == 0)
            {
                obj.transform.localPosition = new Vector3(x, y, 0);
            }
            else
            {
                if (str == ".")
                {
                    x += 26;

                }
                else if (str == "万")
                {
                    x += 50;
                    str = "w";
                }
                else if (str == "亿")
                {
                    x += 50;
                    str = "yi";
                }
                else
                {
                    x += 40;
                }
                obj.transform.localPosition = new Vector3(x, y, 0);
            }
            tempStr = tempStr.Remove(0, 1);
            obj.GetComponent<UISprite>().spriteName = plusLess + str;
            //Debug.Log(obj.GetComponent<UISprite>().spriteName);
            //Debug.Log("tempStr = " + tempStr);
        }
    }

    /// <summary>
    /// 判断天地玄黄的输赢情况并展示
    /// </summary>
    public void ShowResult(int bankerPokerType,List<int> elsePokerTypes)
    {
        int num = 0;
        for (int i = 0; i < elsePokerTypes.Count;i++ )
        {
            if (elsePokerTypes[i] > bankerPokerType)
            {
                winLoseSprites[i].spriteName = "ying";
            }
            else
            {
                winLoseSprites[i].spriteName = "shu";
                num++;
            }
            winLoseSprites[i].gameObject.SetActive(true);
        }
        //通杀
        if(num == 4){
            for (int i = 0; i < winLoseSprites.Count;i++ )
            {
                winLoseSprites[i].gameObject.SetActive(false);
            }
            tongSha.SetActive(true);
        }
    }

    IEnumerator ResetBankerAndChairUser(Nn100GameOverBroadcast newpb)
    {
        EginDN.Instance.bankerInfo = newpb.BankerUserInfo;
        EginDN.Instance.chairUserList = newpb.ChairUserList.ToList();
        yield return new WaitForSeconds(5f);


        //可以申请上下庄
        dnHundredPanel.bankerUpBtn.GetComponent<BoxCollider>().enabled = true;
        dnHundredPanel.bankerDownBtn.GetComponent<BoxCollider>().enabled = true;

        //显示庄家信息
        if (newpb.BankerUserInfo.AvatarId != 0 && newpb.BankerUserInfo.BagCoinCount >= 10000000)
        {
            //bankerInfo.gameObject.SetActive(false);
            bankerInfo.avatar.spriteName = newpb.BankerUserInfo.AvatarId.ToString();
            bankerInfo.avatarBtn.GetComponent<UIButton>().normalSprite = newpb.BankerUserInfo.AvatarId.ToString();
            Debug.Log("新庄家信息：" + newpb.BankerUserInfo.Nickname + "  " + newpb.BankerUserInfo.AvatarId);
            bankerInfo.nameLabel.text = newpb.BankerUserInfo.Nickname;
            bankerInfo.goldLabel.text = EginUser.Instance.ReturnMoney(newpb.BankerUserInfo.BagCoinCount);
            bankerInfo.uid = newpb.BankerUserInfo.Uid;
            bankerInfo.gameObject.SetActive(true);
            StartCoroutine(GetWWWFace(bankerInfo.wxAvatar, newpb.BankerUserInfo.AvatarUrl));
        }

        //倘若自己是庄家显示下庄按钮
        if(newpb.BankerUserInfo.Uid == int.Parse(EginUser.Instance.Uid)){
            dnHundredPanel.bankerUpBtn.SetActive(false);
            dnHundredPanel.bankerDownBtn.SetActive(true);
            dnHundredPanel.isCanBet = false;
            EginDN.Instance.isBanker = true;
            dnGame.SelectBtns.SetActive(false);

            //不能下注
            //for (int i = 0; i < 4;i++ )
            //{
            //    dnHundredPanel.betBtns[i].GetComponent<BoxCollider>().enabled = false;
            //}
        }
        else
        {
            EginDN.Instance.isBanker = false;
            dnHundredPanel.bankerUpBtn.SetActive(true);
            dnHundredPanel.bankerDownBtn.SetActive(false);
        }

        //下座的人移除
        for (int i = 0; i < 4; i++)
        {
            users[i].avatar.gameObject.SetActive(false);
            users[i].goldLabel.text = "上座";
            Debug.Log("清空");
        }

        //显示上座玩家的信息
        for (int i = 0; i < newpb.ChairUserList.Count; i++)
        {
            users[i].avatar.spriteName = newpb.ChairUserList[i].AvatarId.ToString();
            users[i].avatarBtn.GetComponent<UIButton>().normalSprite = newpb.ChairUserList[i].AvatarId.ToString();
            Debug.Log(newpb.ChairUserList[i].Nickname + "   " + newpb.ChairUserList[i].AvatarId);
            users[i].avatar.gameObject.SetActive(true);
            users[i].goldLabel.text = EginUser.Instance.ReturnMoney(newpb.ChairUserList[i].BagCoinCount);
            users[i].uid = newpb.ChairUserList[i].Uid;
            StartCoroutine(GetWWWFace(users[i].wxAvatar, newpb.ChairUserList[i].AvatarUrl));
        }
       
       
        //初始化筹码
        dnGame.InitChips();
        Destroy(cardsPanel);
        Destroy(gameObject);
        
    }

    /// <summary>
    /// 加载微信头像
    /// </summary>
    /// <param name="texture"></param>
    /// <param name="url"></param>
    /// <returns></returns>
    IEnumerator GetWWWFace(UITexture texture, string url)
    {
        if (url != "")
        {
            Debug.Log("加载微信头像");
            WWW www = new WWW(url);
            yield return www;
            if (www.isDone)
            {
                if (www.error == null)
                {

                }
                texture.mainTexture = www.texture;
            }
            else
            {

            }
            www.Dispose();
        }
    }
}
